/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.api.enums.TransferCaseSourceEnum;
import com.icinfo.cloud.provider.punish.config.dto.ConfigDictionaryCaseSourceInfoQueryDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigDictionaryInfoQueryDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigDictionaryInfoMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigDictionaryInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigDictionaryInfoService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigDictionaryCaseSourceInfoVo;
import com.icinfo.cloud.provider.punish.config.vo.ConfigDictionaryInfoVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 数据字典配置表 config_dictionary_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月15日
 */
@Service
@Slf4j
public class ConfigDictionaryInfoServiceImpl extends MyBatisServiceSupport implements IConfigDictionaryInfoService {
    @Resource
    private ConfigDictionaryInfoMapper configDictionaryInfoMapper;

    /**
     * 【V1.0.0】描述：查询指定数据字典列表信息
     *
     * @param queryDto {@link ConfigDictionaryInfoQueryDto}
     * @return {@link List < ConfigDictionaryInfoVo >} 数据字典列表信息
     * @author xiangyuyu
     * @date 2022-02-23 20:40
     */
    @Override
    public List<ConfigDictionaryInfoVo> getConfigDictionaryList(ConfigDictionaryInfoQueryDto queryDto) {
        if (ObjectUtils.isEmpty(queryDto.getIsEffective())) {
            queryDto.setIsEffective(Constant.WHETHER_STRING_YES);
        }
        List<ConfigDictionaryInfoVo> configDictionaryList = configDictionaryInfoMapper.selectConfigDictionaryList(queryDto);
        if (ObjectUtils.isEmpty(configDictionaryList)) {
            configDictionaryList = new ArrayList<>();
        }
        // 取remark为字典名称
        if (Constant.WHETHER_STRING_YES.equals(queryDto.getIsRemarkName())) {
            configDictionaryList.forEach(c -> c.setDicName(c.getRemark()));
        }
        return configDictionaryList;
    }

    /**
     * 【V1.0.0】描述：查询指定数据字典列表信息
     *
     * @param queryDto {@link ConfigDictionaryInfoQueryDto}
     * @return {@link List < ConfigDictionaryInfoVo >} 数据字典列表信息
     * @author xiangyuyu
     * @date 2022-02-23 20:40
     */
    @Override
    public List<ConfigDictionaryCaseSourceInfoVo> getConfigDictionaryCaseSourceList(ConfigDictionaryCaseSourceInfoQueryDto queryDto) {
        if (ObjectUtils.isEmpty(queryDto.getIsEffective())) {
            queryDto.setIsEffective(Constant.WHETHER_STRING_YES);
        }
        String lineCode = queryDto.getLineCode();
        List<ConfigDictionaryCaseSourceInfoVo> configDictionaryList = configDictionaryInfoMapper.selectConfigDictionaryCaseSourceList(queryDto);
        if (ObjectUtils.isEmpty(configDictionaryList)) {
            return new ArrayList<>();
        }
        // 自然资源条线得案件来源单独添加天巡地查来源
        if ("CASE_SOURCE".equals(queryDto.getDicFieldCode()) && !StringUtils.isEmpty(lineCode) && lineCode.contains("A031")) {
                for (ConfigDictionaryCaseSourceInfoVo dictionaryInfoVo : configDictionaryList) {
                    if (TransferCaseSourceEnum.CASE_SOURCE_QT.getCfCode().equals(dictionaryInfoVo.getDicCode())) {
                        queryDto.setDicFieldCode("CASE_SOURCE_REPLENISH_99");
                        List<ConfigDictionaryCaseSourceInfoVo> childrenList = configDictionaryInfoMapper.selectConfigDictionaryCaseSourceList(queryDto);
                        dictionaryInfoVo.setChildren(childrenList);
                    }
                }
        }
        return configDictionaryList;
    }

    /**
     * 根据字典域编码和字典值编码获取对应的字典值名称
     * @param dicFieldCode
     * @param dicCode
     * @return
     */
    @Override
    public ConfigDictionaryInfo getConfigDictionaryByFieldCodeAndDicCode(String dicFieldCode, String dicCode){
        ConfigDictionaryInfo param = new ConfigDictionaryInfo();
        param.setDicFieldCode(dicFieldCode);
        param.setDicCode(dicCode);
        param.setIsEffective("1");
        return configDictionaryInfoMapper.selectOne(param);
    }
}
